﻿function declared(object) { return !(object === undefined);}
function defined(object) {return declared(object) && object != null;}
function required(object, name) {if (! defined(object)) throw new Error(name + " is not defined.");}
function dump(object)
{
	var result= "";
	for(var key in object)
	{
		result += key + " = " + object[key] + "\r\n";
	}
	return result;
}

Transit = function() {};
Transit.Location = function() {};
Transit.Location.Manager = function() {}
var object = Transit.Location.Manager.prototype;
object.location = function(callback) {
	required(callback, "callback");
	jQuery.get("Frontend/GetLocation?googleId=91021591594299598", null, function(data) {
		var ll = data.features[0].geometry.coordinates;
		callback(ll[0], ll[1]);
	}, "json");
}

Transit.Map = function() {};
Transit.Map.Manager = function(container)
{
	required(container, "container");
	var me = this;
	this.layout = {};
	this.layout.image = container.find("img");
	this.layout.buttons = container.find(".button");
	this.layout.buttons.click(function() {
		me.move(this.innerText);
	});
};
var object = Transit.Map.Manager.prototype;
object.getUrl = function(long, lat, callback)
{
	var parent = this.layout.image.parent();
	var width = parent.width();
	var height = parent.height();
	if (width < 100) width = 100;
	if (height < 100) height = 100;
	
	this.params = {lat:lat, long:long, width:width, height:height, zoom:13};
	jQuery.get("/Frontend/GetMapUrl", this.params, function(data)
	{
		callback(data);
	}, "text");
}
object.move = function(direction) {
	if (! defined(this.params)) throw new Error("Last params are not defined.");
	var me = this;
	this.params.direction = direction;
	jQuery.get("/Frontend/Move", this.params, function(data)
	{
		me.setUrl(data.url);
		me.params.lat = data.lat;
		me.params.long = data.long;
		delete me.params.direction;
		//alert(dump(data));
	}, "json");
}

object.setUrl = function(url)
{
	required(url, "url");
	this.layout.image.attr("src", url);
}

function addStopToRecent(routeId, stopId, stopDescription, directionId, directionDescription) {
    var c = document.cookie.split("; ");
    var serialized = encodeURIComponent(routeId + '|' + stopId + '|' + stopDescription + '|' + directionId + '|' + directionDescription);

    for (var i = 0; i < c.length; i++) {
        var sp = c[i].split("=");
        if (sp[0] == 'clickedStops') {
            var cs = sp[1].split(",");
            break;
        }
    }

    if (!cs) {
        cs = [];
    }

    //check to see if stop exists in clickedStops
    for (var i = 0; i < cs.length; i++) {
        if (cs[i] == serialized) {
            //already exists.
            return;
        }
    }
    cs.push(serialized);
    cs = cs.join(",");
    document.cookie = 'clickedStops=' + cs;
}

function removeStopFromRecent(routeId, stopId, stopDescription, directionId, directionDescription) {
    var c = document.cookie.split("; ");
    var serialized = encodeURIComponent(routeId + '|' + stopId + '|' + stopDescription + '|' + directionId + '|' + directionDescription);

    for (var i = 0; i < c.length; i++) {
        var sp = c[i].split("=");
        if (sp[0] == 'clickedStops') {
            var cs = sp[1].split(",");
            break;
        }
    }

    if (!cs || cs.length == 0) {
        cs = [];
    }

    //check to see if stop exists in clickedStops
    for (var i = cs.length - 1; i > -1; i--) {
        console.log(cs[i] + "\n" + serialized);
        if (cs[i] == serialized) {
            cs.splice(i, 1);
        }
    }
    cs = cs.join(",");
    document.cookie = 'clickedStops=' + cs;
    populateStops(ORDER_RECENT);
}

function getCookiedStops() {
    var c = document.cookie.split("; ");
    var cs;
    for (var i = 0; i < c.length; i++) {
        var sp = c[i].split("=");
        if (sp[0] == 'clickedStops') {
            cs = sp[1].split(",");
            break;
        }
    }

    var cookiedStops = [];

    if (!cs || cs[0] == '') {
        cs = [];
    }

    for (var i = 0; i < cs.length; i++) {
        var str = cs[i];
        str = decodeURIComponent(str);
        var spl = str.split("|");
        cookiedStops.push({ routeId: spl[0], stopId: spl[1], stopDescription: spl[2], directionId: spl[3], directionDescription: spl[4] });
    }

    return cookiedStops;
}
